#include "CubeClass.h"
const float maxnum=6.0;

CubeClass::CubeClass(void)
{

	minPoint.x=minPoint.y=minPoint.z=0;
	maxPoint.x=maxPoint.y=maxPoint.z=0;
}

CubeClass::~CubeClass(void)
{
}

static bool intersect_plane(float oi,float vi,float Bmin,float Bmax,float &tmin,float &tmax)
{
	float bmin,bmax;
	float t;
	if (vi==0)
	{
		return false;
	}
	else
	{
		if (vi>0)
		{
			bmin=Bmin;
			bmax=Bmax;
		}
		else
		{
			bmin=Bmax;
			bmax=Bmin;
		}
		if (bmax-oi<0)
		{
			return false;
		}
		t=(bmax-oi)/vi;
		if (t<tmax+0.001)
		{
			if (t<tmin)
			{
				return false;
			}
			tmax=t;
		}
		if (bmin-oi>0)
			t=(bmin-oi)/vi;
		if (t>tmin-0.001)
		{
			if (t>tmax)
			{
				return false;
			}
			tmin=t;
		}
	}
	return true;
}

bool CubeClass::intersect( Point o, Vector u, Point *hit,Point *out )
{
	if (hit==NULL||out==NULL)
	{
		return false;
	}

	hit->x=-maxnum;
	hit->y=-maxnum;
	hit->z=-maxnum;

	out->x=maxnum;
	out->y=maxnum;
	out->z=maxnum;
	if (!intersect_plane(o.x,u.x,this->minPoint.x,this->maxPoint.x,hit->x,out->x))
	{
		return false;
	}
	if (!intersect_plane(o.y,u.y,this->minPoint.y,this->maxPoint.y,hit->y,out->y))
	{
		return false;
	}
	if (!intersect_plane(o.z,u.z,this->minPoint.z,this->maxPoint.z,hit->z,out->z))
	{
		return false;
	}
	return true;
}



